package com.wsoft.hr.pm.controller;

import com.github.pagehelper.PageInfo;
import com.wsoft.hr.pm.entity.HrPmTemplateEntity;
import com.wsoft.hr.pm.query.template.TemplateListQuery;
import com.wsoft.hr.pm.query.template.TemplateSaveQuery;
import com.wsoft.hr.pm.query.template.TemplateStateQuery;
import com.wsoft.hr.pm.service.IHrPmTemplateService;
import com.wsoft.response.ResponsePage;
import com.wsoft.response.ResponseResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 考评 模板表 前端控制器
 *
 * @author Z001
 * @since 2025年02月24日 18:25:38
 */
@RestController
@Tag(name = "模版")
@RequestMapping("/hr/pm/template")
public class HrPmTemplateController {

    @Resource
    private IHrPmTemplateService templateService;


    @Operation(summary = "暂存模版", description = "暂存模版")
    @PostMapping("/tempInsert")
    public ResponseResult tempInsert(@RequestBody TemplateSaveQuery query) {
        Long template = templateService.tempInsert(query, false);
        return ResponseResult.success(templateService.getTemplateDetail(template));
    }

    @Operation(summary = "保存模版", description = "保存模版")
    @PostMapping("/insert")
    public ResponseResult insert(@RequestBody @Valid TemplateSaveQuery query) {
        Long template = templateService.insert(query);
        return ResponseResult.success(templateService.getTemplateDetail(template));
    }

    @Operation(summary = "修改模版状态", description = "修改模版状态")
    @PostMapping("/updateState")
    public ResponseResult updateState(@RequestBody @Valid TemplateStateQuery query) {
        boolean b = templateService.updateState(query);
        if (b) {
            return ResponseResult.success("[sys.msg.success]");
        } else {
            return ResponseResult.failed("[sys.msg.failed]");
        }
    }

    @Operation(summary = "删除模版", description = "删除模版")
    @GetMapping("/delete")
    public ResponseResult delete(@RequestParam("id") Long id) {
        boolean b = templateService.delete(id);
        if (b) {
            return ResponseResult.success("[sys.msg.success]");
        } else {
            return ResponseResult.failed("[sys.msg.failed]");
        }
    }

    @Operation(summary = "模版分页", description = "模版分页")
    @PostMapping("/list")
    public ResponseResult list(@RequestBody TemplateListQuery query) {
        PageInfo<HrPmTemplateEntity> page = templateService.listTemplate(query);
        return ResponseResult.success(ResponsePage.restPage(page));
    }

    @Operation(summary = "模版详情", description = "模版详情")
    @GetMapping("/get")
    public ResponseResult get(@RequestParam("id") Long id) {
        return ResponseResult.success(templateService.getTemplateDetail(id));
    }


    @Operation(summary = "复制模板", description = "复制模板")
    @GetMapping("/copyTemplate")
    public ResponseResult copyTemplate(@RequestParam("id") Long id) {
        boolean b = templateService.copyTemplate(id);
        if (b) {
            return ResponseResult.success("[sys.msg.success]");
        } else {
            return ResponseResult.failed("[sys.msg.failed]");
        }
    }


}
